﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.DKChung;

namespace QuanLyKho.Control
{
    public partial class DKChung_DkAnh : UserControl
    {
        public DKChung_DkAnh()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            var tuNgay = dtTuNgay.Checked ? dtTuNgay.Value.ToString("MM/dd/yyyy") : DateTime.Now.Month + "/01/" + DateTime.Now.Year;
            var denNgay = dtDenNgay.Checked ? dtDenNgay.Value.ToString("MM/dd/yyyy") : DateTime.Now.Month + "/"
                + DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month) + "/" + DateTime.Now.Year;
            var table = DataAll.SelectAll("DkChung_HinhAnh", "[IDHinhAnh],[MaAnh],[TenAnh],[NgayTao],[Loai],[GhiChu]"
                , "WHERE (CONVERT(DATE, NgayTao, 0) >= CONVERT(DATE,'"
                + tuNgay + "', 0) AND CONVERT(DATE, NgayTao, 0) <= CONVERT(DATE,'" + denNgay + "' , 0)) AND Loai =N'0'");
            gridHinhAnh.DataSource = table;
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            //gvNhomHang.BestFitColumns();
            gvHinhAnh.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvHinhAnh.OptionsBehavior.Editable = false;
            gvHinhAnh.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvHinhAnh.OptionsSelection.MultiSelect = true;
            gvHinhAnh.OptionsView.ShowGroupPanel = false;
            gvHinhAnh.OptionsView.ColumnAutoWidth = true;
            gvHinhAnh.OptionsView.ShowFooter = true;
            gvHinhAnh.OptionsFind.AlwaysVisible = true;
            gvHinhAnh.Columns["IDHinhAnh"].Visible = false;
        }

        private void bntThemMoi_Click(object sender, EventArgs e)
        {
            DKChung_DkHinhAnh.Loai = 0;
            var dangkyanh = new DKChung_DkHinhAnh();
            dangkyanh.ShowDialog();
            LoadData();
        }

        private void gvHinhAnh_DoubleClick(object sender, EventArgs e)
        {
            if (gvHinhAnh.FocusedRowHandle>=0)
            {
                DKChung_DkHinhAnh.MaID = gvHinhAnh.Columns["IDHinhAnh"].View.GetRowCellDisplayText(gvHinhAnh.FocusedRowHandle, "IDHinhAnh");
                var dkhinhanh = new DKChung_DkHinhAnh();
                dkhinhanh.update = true;
                dkhinhanh.ShowDialog();
                LoadData();
                DKChung_DkHinhAnh.MaID = "0";
            }
        }

        private void gvHinhAnh_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "Loai")
            {
                e.DisplayText =  (string) e.Value == "0" ? "Sản phẩm" : "Vật tư";
            }
        }

        private void dtTuNgay_Leave(object sender, EventArgs e)
        {
            LoadData();
        }

        private void dtDenNgay_Leave(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
